package com.ear.orange.controller;

import com.ear.orange.common.Result;
import com.ear.orange.entity.ShoppingCart;
import com.ear.orange.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 *
 * @author orange
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("购物车添加商品")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}", shoppingCart);
        ShoppingCart res = shoppingCartService.addProduct(shoppingCart);
        return Result.success(res);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("购物车展示商品")
    public Result<List<ShoppingCart>> list() {
        log.info("查看购物车。。。");
        List<ShoppingCart> list = shoppingCartService.listProducts();
        return Result.success(list);
    }

    /**
     * 减少商品
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("购物车清空商品")
    public Result<String> clean() {
        shoppingCartService.cleanProducts();
        return Result.success("清空购物车成功！");
    }

    /**
     * 减少购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("购物车减少商品")
    public Result<ShoppingCart> sub2(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}", shoppingCart);
        ShoppingCart result = shoppingCartService.decreaseProduct(shoppingCart);
        return Result.success(result);
    }

}
